var path = require('path'),
    webpack = require('webpack'),
    fs = require('fs'),
    config = require('./config'),
    webpackRoutes = require('./server/route/webpack'),
    indexRoutes = require('./server/route/index'),
    oauthRoutes = require('./server/route/oauth'),
    srcDir = path.join(process.cwd(), 'src'), // process.cwd()：返回运行当前脚本的工作目录的路径
    imagePath = path.join(srcDir, 'img'),
    cssPath = path.join(srcDir, 'css'),
    jsPath = path.join(srcDir, 'js'),
    viewsPath = path.join(srcDir, 'views'),
    nodeModulesPath = path.join(__dirname, 'node_modules'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    publicPath = config.domain + '/',
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    pxtorem = require('postcss-pxtorem'),
    autoprefixer = require('autoprefixer');

module.exports = {
    cache: true,
    entry: {
        index: path.join(__dirname, "src/views/index.jsx"),
    },
    root: srcDir,
    output: {
        publicPath: publicPath,
        path: path.join(__dirname, "build/static/"), //文件输出目录
        filename: "core/[name].js", //根据入口文件输出的对应多个文件名
        chunkFilename: "core/[name].js"
    },
    resolve: {
        extensions: ['', '.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.json'],
        alias: { //配置别名，在项目中可缩减引用路径
            'img': imagePath,
            'css': cssPath,
            'js': jsPath,
            'views': viewsPath
        },
        modulesDirectories: ["node_modules"]
    },
    devServer: {
        historyApiFallback: true,
        contentBase: 'build/static/',
        hot: true,
        inline: true,
        progress: true,
        port: config.port,
        setup: function (app) {
            webpackRoutes(app);
            //oauthRoutes(app);
            //indexRoutes(app);
        },
    },
    module: {
        noParse: [/jquery/],
        loaders: [{
            test: /\.(png|jpg|gif)$/,
            include: [imagePath],
            loader: "url-loader?limit=1000&name=img/[name].[hash:5].[ext]"
        }, {
            test: /\.css$/,
            include: /node_modules/,
            loader: "style!css!postcss"
        }, {
            test: /\.scss$/,
            loaders: ["style-loader", "css-loader?sourceMap", "postcss-loader", "sass-loader?sourceMap"],
        }, {
            test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'svg-sprite',
            include: [require.resolve('antd-mobile').replace(/warn\.js$/, '')],
        }, {
            test: /\.jsx$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['react', 'es2015'],
                env: {
                    development: {
                        presets: ["react-hmre"]
                    }
                },
                "plugins": [
                    ["import", {
                        "libraryName": "antd-mobile",
                        "style": "css"
                    }]
                ]
            }
        }, {
            test: /\.js$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['es2015']
            }
        }]
    },
    postcss: [autoprefixer({
        browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
    }), pxtorem({
        rootValue: 100,
        propWhiteList: [],
    })],
    sassLoader: {
        imagePath: imagePath,
        includePaths: [cssPath]
    },
    plugins: [
        new webpack.DllReferencePlugin({ // 加快webpack打包速度
            context: __dirname,
            manifest: require('./build/static/vendor-manifest.json')
        }),
        new HtmlWebpackPlugin({
            template: path.join(__dirname, 'build/html/index_dll.html'),
            inject: true
        })
    ]
};